import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
    }]
  },

  {
    path: '/demo',
    component: () => import ('@/components/demo/demo'),
    name: '组件测试',
    meta: {
      title: '组件测试'
    }
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/star',
    component: Layout,
    // redirect: '/nested/menu1',
    name: '校园明星',
    meta: {
      title: '校园明星',
      icon: 'nested',
      roles: ['2', '3', '4']
    },
    children: [
      {
        path: 'submit',
        component: () => import('@/components/star/create_star'), // Parent router-view
        name: '校园明星申报',
        meta: { title: '校园明星申报' }
      },
      {
        path: 'verify',
        component: () => import('@/views/star/audit_star'),
        name: '校园明星审核',
        meta: {
          title: '校园明星审核',
          roles: ['4']
        }
      },
      {
        path: 'list/part',
        component: () => import('@/views/star/part_star'),
        name: '本学部明星列表',
        meta: {
          title: '本学部明星列表',
          roles: ['2', '3']
        }
      },
      {
        path: 'list',
        component: () => import('@/views/star/school_star'),
        name: '校园明星列表',
        meta: {
          title: '校园明星列表',
          roles: ['3']
        }
      },
      {
        path: 'list/manage',
        component: () => import('@/views/star/manage_star'),
        name: '校园明星管理',
        meta: {
          title: '校园明星管理',
          roles: ['4']
        }
      }
    ]
  },

  {
    path: '/activity',
    component: Layout,
    meta: {
      title: '校园活动',
      icon: 'nested',
      roles: ['2', '3', '4']
    },
    children: [
      {
        path: 'submit',
        component: () => import('@/components/activity/create_activity'), // Parent router-view
        name: '校园活动申报',
        meta: { title: '校园活动申报' }
      },
      {
        path: 'verify/part',
        component: () => import('@/views/activity/audit_activity'),
        name: '学部活动审核 (活动类别+活动状态接口)',
        meta: {
          title: '学部活动审核',
          roles: ['3']
        }
      },
      {
        path: 'verify/school',
        component: () => import('@/views/activity/audit_schoolActivity'),
        name: '校园活动审核',
        meta: {
          title: '校园活动审核',
          roles: ['4']
        }
      },

      {
        path: 'list/part',
        component: () => import('@/views/activity/part_activity'),
        name: '学部活动列表',
        meta: {
          title: '学部活动列表',
          roles: ['2', '3']
        }
      },
      {
        path: 'list/school',
        component: () => import('@/views/activity/school_activity'),
        name: '校园活动列表',
        meta: {
          title: '校园活动列表',
          roles: ['4']
        }
      },
      {
        path: 'list/my',
        component: () => import('@/views/activity/my_activity'),
        name: '我负责的活动',
        meta: {
          title: '我负责的活动',
          roles: ['2', '3', '4']
        }
      }

    ]
  },

  {
    path: '/info',
    component: Layout,
    name: '学生信息',
    meta: {
      title: '学生信息',
      icon: 'nested',
      roles: ['2', '3', '4']
    },
    children: [
      {
        path: 'search',
        component: () => import('@/views/user_info/user_info'),
        name: '信息查找',
        meta: {
          title: '信息查找',
          roles: ['2', '3', '4']
        }
      }
    ]

  },

  {
    path: '/job',
    component: Layout,
    // redirect: '/nested/menu1',
    name: '岗位内推',
    meta: {
      title: '岗位内推',
      icon: 'nested',
      roles: ['2', '3', '4']
    },
    children: [
      {
        path: 'submit',
        component: () => import('@/components/job/create_job'), // Parent router-view
        name: '岗位内推发布',
        meta: { title: '岗位内推发布' }
      },
      {
        path: 'list/school',
        component: () => import('@/views/job/school_job'),
        name: '校园岗位内推列表',
        meta: {
          title: '校园岗位内推列表',
          roles: ['4']
        }
      },
      {
        path: 'list/part',
        component: () => import('@/views/job/part_job'),
        name: '学部岗位内推列表',
        meta: {
          title: '学部岗位内推列表',
          roles: ['3']
        }
      },
      {
        path: 'list/partlist',
        component: () => import('@/views/job/part_job_list'),
        name: '学部岗位内推列表',
        meta: {
          title: '学部岗位内推列表',
          roles: ['2']
        }
      }
    ]
  },
  {
    path: '/news',
    component: Layout,
    // redirect: '/nested/menu1',
    name: '校园新闻',
    meta: {
      title: '校园新闻',
      icon: 'nested',
      roles: ['2', '3', '4']
    },
    children: [
      {
        path: 'submit',
        component: () => import('@/components/news/create_news'), // Parent router-view
        name: '校园新闻发布',
        meta: { title: '校园新闻发布' }
      },
      {
        path: 'list/school',
        component: () => import('@/views/news/school_news'),
        name: '校园新闻列表',
        meta: {
          title: '校园新闻列表',
          roles: ['4']
        }
      },
      {
        path: 'list/part',
        component: () => import('@/views/news/part_news'),
        name: '学部新闻列表',
        meta: {
          title: '学部新闻列表',
          roles: ['2', '3']
        }
      },
      {
        path: 'list/my',
        component: () => import('@/views/news/my_news'),
        name: '我负责的新闻',
        meta: {
          title: '我负责的新闻',
          roles: ['3']
        }
      }
    ]
  },

  {
    path: '/moment',
    component: Layout,
    name: '精彩瞬间',
    meta: {
      title: '精彩瞬间',
      icon: 'nested',
      roles: ['2', '3', '4']
    },
    children: [
      {
        path: 'momnet/add',
        component: () => import('@/views/moment/addMoment'),
        name: '添加精彩瞬间',
        meta: { title: '添加精彩瞬间' }
      },
      {
        path: 'momnet',
        component: () => import('@/views/moment/moment'), // Parent router-view
        name: '精彩瞬间列表',
        meta: { title: '精彩瞬间列表' }
      }
    ]
  },
  {
    path: '/knowledge',
    component: Layout,
    name: '知识分享',
    meta: {
      title: '知识分享',
      icon: 'nested',
      roles: ['2', '3', '4']
    },
    children: [
      // {
      //   path: 'add',
      //   component: () => import('@/views/knowledge/addKnowledge'),
      //   name: '添加知识分享',
      //   meta: { title: '添加知识分享' }
      // },
      {
        path: 'knowledge',
        component: () => import('@/views/knowledge/knowledge'), // Parent router-view
        name: '知识分享列表',
        meta: { title: '知识分享列表' }
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
